<?php
/**
 * 帮助类，一些需要用到的公共函数
 * User: yuanji
 * Date: 2018/7/8
 * Time: 16:01
 */

namespace Lib;

class Http
{
    /**
     * Curl send get 请求，支持HTTPS协议
     * @param string $url 请求url
     * @param $data array get请求的参数 键值对
     * @param string $refer 设置来源地址
     * @param int $timeout The timeout seconds
     * @param array $header 设置请求头
     * @return mixed
     */
    public static function get($url, $data, $refer = "", $timeout = 10, $header = [])
    {
        $ssl = stripos($url, 'https://') === 0 ? true : false;
        $curlObj = curl_init();
        $options = [
            CURLOPT_URL => $url. '?' . http_build_query($data),
            CURLOPT_RETURNTRANSFER => 1,//设为TRUE把curl_exec()结果转化为字串，而不是直接输出
            CURLOPT_FOLLOWLOCATION => 1,
            //CURLOPT_POSTFIELDS => $data,
            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_AUTOREFERER => 1,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_NONE,
            CURLOPT_HTTPHEADER => ['Expect:'],//设置 HTTP 头字段的数组。格式： array('Content-type: text/plain', 'Content-length: 100')
            CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
            CURLOPT_CUSTOMREQUEST => 'GET',//设置请求方式
        ];
        if ($refer) {
            $options[CURLOPT_REFERER] = $refer;//设置请求参数
        }
        if (!empty($header)) {
            $options[CURLOPT_HTTPHEADER] = $header;
        }
        if ($ssl) {
            //support https
            $options['CURLOPT_SSL_VERIFYHOST'] = false;
            $options['CURLOPT_SSL_VERIFYPEER'] = false;
        }
        curl_setopt_array($curlObj, $options);
        $returnData = curl_exec($curlObj);
        if (curl_errno($curlObj)) {
            //error message
            $returnData = curl_error($curlObj);
        }
        curl_close($curlObj);
        return $returnData;
    }

    /**
     * Curl send post request, support HTTPS protocol
     * @param string $url The request url
     * @param array $data The post data
     * @param string $refer The request refer
     * @param int $timeout The timeout seconds
     * @param array $header The other request header
     * @return mixed
     */
    public static function post($url, $data, $refer = "", $timeout = 10, $header = [])
    {
        $curlObj = curl_init();
        $ssl = stripos($url, 'https://') === 0 ? true : false;
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => 1,//设为TRUE把curl_exec()结果转化为字串，而不是直接输出
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_AUTOREFERER => 1,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
            CURLOPT_HTTPHEADER => ['Expect:'],
            CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
            CURLOPT_REFERER => $refer,
            CURLOPT_CUSTOMREQUEST => 'POST',//设置请求方式
        ];
        if (!empty($header)) {
            $options[CURLOPT_HTTPHEADER] = $header;
        }
        if ($refer) {
            $options[CURLOPT_REFERER] = $refer;
        }
        if ($ssl) {
            //support https
            $options[CURLOPT_SSL_VERIFYHOST] = false;
            $options[CURLOPT_SSL_VERIFYPEER] = false;
        }
        curl_setopt_array($curlObj, $options);
        $returnData = curl_exec($curlObj);
        if (curl_errno($curlObj)) {
            //error message
            $returnData = curl_error($curlObj);
        }
        curl_close($curlObj);
        return $returnData;
    }

    /**
     * Curl send put request, support HTTPS protocol
     * @param string $url The request url
     * @param array $data The post data
     * @param string $refer The request refer
     * @param int $timeout The timeout seconds
     * @param array $header The other request header
     * @return mixed
     */
    public static function put($url, $data, $refer = "", $timeout = 10, $header = [])
    {
        //$data = json_encode($data);
        $curlObj = curl_init();
        $ssl = stripos($url, 'https://') === 0 ? true : false;
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => 1,//设为TRUE把curl_exec()结果转化为字串，而不是直接输出
            CURLOPT_POSTFIELDS => http_build_query($data),
            //CURLOPT_FOLLOWLOCATION => 1,//TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。（注意：这是递归的，"Location: " 发送几次就重定向几次，除非设置了 CURLOPT_MAXREDIRS，限制最大重定向次数。）。
            //CURLOPT_AUTOREFERER => 1,//TRUE 时将根据 Location: 重定向时，自动设置 header 中的Referer:信息。
            CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
            CURLOPT_HTTPHEADER => ['Expect:'],
            CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
            CURLOPT_REFERER => $refer,
            CURLOPT_CUSTOMREQUEST => 'PUT',//设置请求方式
        ];
        if (!empty($header)) {
            $options[CURLOPT_HTTPHEADER] = $header;
        }
        if ($refer) {
            $options[CURLOPT_REFERER] = $refer;
        }
        if ($ssl) {
            //support https
            $options[CURLOPT_SSL_VERIFYHOST] = false;
            $options[CURLOPT_SSL_VERIFYPEER] = false;
        }
        curl_setopt_array($curlObj, $options);
        $returnData = curl_exec($curlObj);
        if (curl_errno($curlObj)) {
            //error message
            $returnData = curl_error($curlObj);
        }
        curl_close($curlObj);
        return $returnData;
    }

    /**
     * Curl send del request, support HTTPS protocol
     * @param string $url The request url
     * @param array $data The post data
     * @param string $refer The request refer
     * @param int $timeout The timeout seconds
     * @param array $header The other request header
     * @return mixed
     */
    public static function del($url, $data, $refer = "", $timeout = 10, $header = [])
    {
        $curlObj = curl_init();
        $ssl = stripos($url, 'https://') === 0 ? true : false;
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => 1,//设为TRUE把curl_exec()结果转化为字串，而不是直接输出
            CURLOPT_POSTFIELDS => http_build_query($data),
            //CURLOPT_FOLLOWLOCATION => 1,//TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。（注意：这是递归的，"Location: " 发送几次就重定向几次，除非设置了 CURLOPT_MAXREDIRS，限制最大重定向次数。）。
            //CURLOPT_AUTOREFERER => 1,//TRUE 时将根据 Location: 重定向时，自动设置 header 中的Referer:信息。
            CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)',
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
            CURLOPT_HTTPHEADER => ['Expect:'],
            CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
            CURLOPT_REFERER => $refer,
            CURLOPT_CUSTOMREQUEST => 'DELETE',//设置请求方式
        ];
        if (!empty($header)) {
            $options[CURLOPT_HTTPHEADER] = $header;
        }
        if ($refer) {
            $options[CURLOPT_REFERER] = $refer;
        }
        if ($ssl) {
            //support https
            $options[CURLOPT_SSL_VERIFYHOST] = false;
            $options[CURLOPT_SSL_VERIFYPEER] = false;
        }
        curl_setopt_array($curlObj, $options);
        $returnData = curl_exec($curlObj);
        if (curl_errno($curlObj)) {
            //error message
            $returnData = curl_error($curlObj);
        }
        curl_close($curlObj);
        return $returnData;
    }
}